Completed
Push — master ( e8f96e...301a71 )
by Maxence
17s
created

actions.createCircleResult   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 0
f 0
cc 3
nc 3
nop 1
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
/** global: actions */
31
/** global: nav */
32
/** global: elements */
33
/** global: curr */
34
/** global: api */
35
36
37
var actions = {
38
39
40
	joinCircleResult: function (result) {
41
		if (result.status === 0) {
42
			OCA.notification.onFail(
43
				t('circles', "Cannot join this circle") + ': ' +
44
				((result.error) ? result.error : t('circles', 'no error message')));
45
			return;
46
		}
47
48
		elements.removeMemberslistEntry(result.member.user_id);
49
		if (result.member.level === 1) {
50
			OCA.notification.onSuccess(
51
				t('circles', "You have successfully joined this circle"));
52
		} else {
53
			OCA.notification.onSuccess(
54
				t('circles', "You have requested to join this circle"));
55
		}
56
		actions.selectCircle(result.circle_id);
57
	},
58
59
60
	leaveCircleResult: function (result) {
61
		if (result.status === 1) {
62
63
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
64
				function () {
65
					$(this).hide(300);
66
				});
67
68
			actions.selectCircle(result.circle_id);
69
			OCA.notification.onSuccess(
70
				t('circles', "You have successfully left this circle"));
71
			return;
72
		}
73
74
		OCA.notification.onFail(
75
			t('circles', "Cannot leave this circle") + ': ' +
76
			((result.error) ? result.error : t('circles', 'no error message')));
77
	},
78
79
80
	destroyCircleResult: function (result) {
81
		if (result.status === 1) {
82
83
			actions.unselectCircle(result.circle_id);
84
			OCA.notification.onSuccess(
85
				t('circles', "You have successfully deleted this circle"));
86
			return;
87
		}
88
89
		OCA.notification.onFail(
90
			t('circles', "Cannot delete this circle") + ': ' +
91
			((result.error) ? result.error : t('circles', 'no error message')));
92
	},
93
94
95
	createCircleResult: function (result) {
96
		var type = actions.getStringTypeFromType(result.type);
97
98
		if (result.status === 1) {
99
			OCA.notification.onSuccess(t('circles', " {type} '{name}' created", {
100
				type: type,
101
				name: result.name
102
			}));
103
			elements.emptyCircleCreation();
104
			nav.displayCirclesList(result.circle.type);
105
			actions.selectCircle(result.circle.id);
106
			return;
107
		}
108
109
		OCA.notification.onFail(
110
			t('circles', " {type} '{name}' could not be created", {
111
				type: type,
112
				name: result.name
113
			}) + ': ' +
114
			((result.error) ? result.error : t('circles', 'no error message')));
115
	},
116
117
118
	selectCircleResult: function (result) {
119
120
		elements.mainUIMembers.emptyTable();
121
		if (result.status < 1) {
122
			OCA.notification.onFail(
123
				t('circles', 'Issue while retrieving the details of this circle') + '" ' +
124
				((result.error) ? result.error : t('circles', 'no error message')));
125
			return;
126
		}
127
128
		elements.navigation.children('.circle').removeClass('selected');
129
		elements.navigation.children(".circle[circle-id='" + result.circle_id + "']").each(
130
			function () {
131
				$(this).addClass('selected');
132
			});
133
134
		elements.emptyContent.hide(800);
135
		elements.mainUI.fadeIn(800);
136
		curr.circle = result.circle_id;
137
		curr.circleLevel = result.details.user.level;
138
139
		nav.displayCircleDetails(result.details);
140
		nav.displayMembersInteraction(result.details);
141
		nav.displayMembers(result.details.members);
142
	},
143
144
145
	listCirclesResult: function (result) {
146
147
		if (result.status < 1) {
148
			OCA.notification.onFail(
149
				t('circles', 'Issue while retrieving the list of circles') + '; ' +
150
				((result.error) ? result.error : t('circles', 'no error message')));
151
			return;
152
		}
153
154
		elements.resetCirclesList();
155
156
		var data = result.data;
157
		for (var i = 0; i < data.length; i++) {
158
			var tmpl = elements.generateTmplCircle(data[i]);
159
			elements.navigation.append(
160
				'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
161
		}
162
163
		elements.navigation.children('.circle').on('click', function () {
164
			actions.selectCircle($(this).attr('circle-id'));
165
		});
166
	},
167
168
	linkCircleResult: function (result) {
169
170
		//elements.linkCircle.val('');
171
		if (result.status !== 1) {
172
			OCA.notification.onFail(
173
				t('circles', "A link to <b>{remote}</b> could not be initiated", {
174
					remote: result.remote
175
				}) + ': ' +
176
				((result.error) ? result.error : t('circles', 'no error message')));
177
			return;
178
		}
179
180
		if (result.link.status === 6) {
181
			OCA.notification.onSuccess(
182
				t('circles', "A link to <b>{remote}</b> has been requested.", {
183
					remote: result.remote
184
				}));
185
		}
186
187
		if (result.link.status === 9) {
188
			OCA.notification.onSuccess(
189
				t('circles', "the link to <b>{remote}</b> is now up and running.", {
190
					remote: result.remote
191
				}));
192
		}
193
194
	},
195
196
197
	selectCircle: function (circle_id) {
198
		curr.searchUser = '';
199
		elements.addMember.val('');
200
		elements.linkCircle.val('');
201
202
		api.detailsCircle(circle_id, actions.selectCircleResult);
203
	},
204
205
206
	unselectCircle: function (circle_id) {
207
		elements.mainUIMembers.emptyTable();
208
		elements.navigation.children(".circle[circle-id='" + circle_id + "']").remove();
209
		elements.emptyContent.show(800);
210
		elements.mainUI.fadeOut(800);
211
212
		curr.circle = 0;
213
		curr.circleLevel = 0;
214
	},
215
216
217
	/**
218
	 *
219
	 * @param search
220
	 */
221
	searchMembersRequest: function (search) {
222
223
		if (curr.searchUser === search) {
224
			return;
225
		}
226
227
		curr.searchUser = search;
228
229
		$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
230
			{
231
				format: 'json',
232
				search: search,
233
				perPage: 200,
234
				itemType: 'principals'
235
			}, actions.searchMembersResult);
236
	},
237
238
239
	searchMembersResult: function (response) {
240
241
		elements.membersSearchResult.children().remove();
242
243
		if (response === null ||
244
			(response.ocs.data.users.length === 0 && response.ocs.data.exact.users.length === 0)) {
245
			elements.membersSearchResult.fadeOut(0);
246
			return;
247
		}
248
249
		elements.fillMembersSearch(response.ocs.data.exact.users, response.ocs.data.users);
250
251
		$('.members_search').on('click', function () {
252
			api.addMember(curr.circle, $(this).attr('searchresult'),
253
				actions.addMemberResult);
254
		});
255
		elements.membersSearchResult.fadeIn(300);
256
	},
257
258
259
	addMemberResult: function (result) {
260
261
		if (result.status === 1) {
262
			OCA.notification.onSuccess(
263
				t('circles', "Member '{name}' successfully added to the circle",
264
					{name: result.name}));
265
266
			nav.displayMembers(result.members);
267
			return;
268
		}
269
		OCA.notification.onFail(
270
			t('circles', "Member '{name}' could not be added to the circle", {name: result.name}) +
271
			': ' +
272
			((result.error) ? result.error : t('circles', 'no error message')));
273
	},
274
275
276
	getStringTypeFromType: function (type) {
277
		switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
278
			case '1':
279
				return t('circles', 'Personal circle');
280
			case '2':
281
				return t('circles', 'Hidden circle');
282
			case '4':
283
				return t('circles', 'Private circle');
284
			case '8':
285
				return t('circles', 'Public circle');
286
		}
287
288
		return t('circles', 'Circle');
289
	},
290
291
292
	removeMemberResult: function (result) {
293
		if (result.status === 1) {
294
295
			elements.rightPanel.fadeOut(300);
296
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
297
				function () {
298
					$(this).hide(300);
299
				});
300
			OCA.notification.onSuccess(
301
				t('circles', "Member '{name}' successfully removed from the circle",
302
					{name: result.name}));
303
			return;
304
		}
305
306
		OCA.notification.onFail(
307
			t('circles', "Member '{name}' could not be removed from the circle",
308
				{name: result.name}) +
309
			': ' +
310
			((result.error) ? result.error : t('circles', 'no error message')));
311
	},
312
313
314
	/**
315
	 *
316
	 */
317
	onEventNewCircle: function () {
318
		curr.circle = 0;
319
		curr.circleLevel = 0;
320
321
		elements.circlesList.children('div').removeClass('selected');
322
		elements.emptyContent.show(800);
323
		elements.mainUI.fadeOut(800);
324
	},
325
326
327
	/**
328
	 *
329
	 */
330
	onEventNewCircleName: function () {
331
		this.onEventNewCircle();
332
		nav.displayOptionsNewCircle((elements.newName.val() !== ''));
333
	},
334
335
336
	/**
337
	 *
338
	 */
339
	onEventNewCircleType: function () {
340
		this.onEventNewCircle();
341
		elements.newTypeDefinition.children('div').fadeOut(300);
342
		var selectedType = elements.newType.children('option:selected').val();
343
		if (selectedType === '') {
344
			elements.newType.addClass('select_none');
345
		}
346
		else {
347
			elements.newType.removeClass('select_none');
348
			$('#circles_new_type_' + selectedType).fadeIn(
349
				300);
350
		}
351
	}
352
353
354
};
355